package uia.alumni.group;

import java.io.IOException;
import java.util.List;
import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.persistence.Query;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.transaction.UserTransaction;
import uia.alumni.data.Role;
import uia.alumni.data.User;
import uia.alumni.web.Command;
import uia.alumni.web.Home;



/**
 *
 * @author Even Åby Larsen (even.larsen@uia.no)
 */
public class CreateGroup extends Command implements Constants {
    /** SERVLET_NAME is used for consistency checking of web.xml */
    public static final String SERVLET_NAME = "group.create";

    @PersistenceUnit(name=PERSISTENCE_UNIT)
    private EntityManagerFactory emf;

    @Resource
    private UserTransaction tx;


    public CreateGroup() {
        super(null);
    }


    /**
     * Creates a new user with data from the Register form.
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */

    /**
    public void execute(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        String email = request.getParameter(USERNAME);
        String[] password = request.getParameterValues(PASSWORD);
        boolean ok = true;

        if (null == email || "".equals(email)) {
            ok = false;
            addMessage(request, "You must enter your email address!");
        }
        if (password.length != 2 || "".equals(password[0])) {
            ok = false;
            addMessage(request, "You must enter your password twice!");
        }
        else if (! password[0].equals(password[1])) {
            ok = false;
            addMessage(request, "Password not confirmed!");
        }

        EntityManager em = emf.createEntityManager();
        Query q = em.createNamedQuery("User.findByName");
        q.setParameter("name", email);
        List<User> l = (List<User>)q.getResultList();

        if (l != null && !l.isEmpty()) {
            ok = false;
            addMessage(request, "A profile already exist for "+email+"!");
        }

        if (ok) {
            try {
                tx.begin();
                em.joinTransaction();
                User user = new User(email, password[0]);
                user.addRole(Role.member);
                em.persist(user);
                tx.commit();
                request.getSession(true).setAttribute(USER, user);
                addMessage(request, "Profile created. Now please complete it!");
                forwardTo(EditProfile.SERVLET_NAME, request, response);
            }
            catch (Exception e) {
                addMessage(request, "Creating the profile failed, because "+e.getMessage());
                forwardTo(Home.SERVLET_NAME, request, response);
            }
        }
        else {
            forwardTo(Register.SERVLET_NAME, request, response);
        }
    }

    public final static long serialVersionUID = 1;

}

      */